/**
 * Created by zhaojm on 15/3/28.
 */
game.GameLayer = cc.Layer.extend({
    _bgLayer : null,
    _ship : null,
    _hubLayer : null,
    _uiLayer : null,

    play_bullet : null,
    enemy_bullet : null,
    enemy_items: null,
    _time : null,
    ctor:function(){
        this._super();

        this.play_bullet = [];
        this.enemy_bullet = [];
        this.enemy_items = [];
        this._time = 0;

        game.Config.init();

        cc.spriteFrameCache.addSpriteFrames(res.bullet_plist);
        cc.spriteFrameCache.addSpriteFrames(res.enemy_plist);
        cc.spriteFrameCache.addSpriteFrames(res.explosion_plist);
        cc.spriteFrameCache.addSpriteFrames(res.ship_plist);


        this._bgLayer = new game.BackgroundLayer();
        this.addChild(this._bgLayer);

        game.Globals.ship = this._ship = new game.Ship();
        this._ship.addToLayer(this);

        game.Globals.hubLayer = this._hubLayer = new game.HubLayer();
        this.addChild(this._hubLayer);

        this._uiLayer = new game.UILayer();
        this.addChild(this._uiLayer);



        this.scheduleUpdate();
        //
        this.schedule(this.scoreCounter, 1);

        //if (Config::sharedConfig()->getAudioState()) {
        //    SimpleAudioEngine::sharedEngine()->playBackgroundMusic(s_bgMusic, true);
        //}

        return true;

    },

    update:function(dt){
        this._super();

        if(this._ship){
            this._ship.update(dt);
        }

        for(var i = 0; i < this.play_bullet.length; i++){
            this.play_bullet[i].update(dt);
        }
        for(var i = 0; i < this.enemy_bullet.length; i++){
            this.enemy_bullet[i].update(dt);
        }

        for(var i = 0; i < this.enemy_items.length; i++){
            this.enemy_items[i].update(dt);
        }


        //if (this._state == statePlaying) {
            this.checkIsCollide();

            this.checkIsReborn();
            this._uiLayer.updateUI();
        //}


    },

    scoreCounter:function(){
        this._time++;
        this.checkEnemyInfo(this._time);
    },

    checkEnemyInfo : function(time){
        var enemy_info = game.Config.enemy_info;
        for(var i = 0; i < enemy_info.length; i++){
            var info = enemy_info[i];
            if(time % info.showTime == 0){
                for(var j = 0; j < info.types.length; j++){
                    var type = info.types[j];

                    if(this.enemy_items.length < 20){
                        var enemy = new game.Enemy(game.Config.getEnemyType(type));
                        enemy.addToLayer(this);
                    }

                }
            }

        }
    },



    checkIsCollide:function() {
        for(var i = 0; i < this.enemy_items.length; i++){
            var enemy_item = this.enemy_items[i];
            for(var j = 0; j < this.play_bullet.length; j++){
                var bullet = this.play_bullet[j];
                if(this.collide(enemy_item, bullet)){
                    enemy_item.hurt();
                    bullet.hurt();
                }
            }

            if(this.collide(enemy_item, this._ship)){
                enemy_item.hurt();
                this._ship.hurt();
            }
        }

        for(var i = 0; i < this.enemy_bullet.length; i++){
            var bullet = this.enemy_bullet[i];
            if(this.collide(bullet, this._ship)){
                bullet.hurt();
                this._ship.hurt();
            }
        }
    },

    checkIsReborn:function() {
        if (game.Config.getLifeCount() > 0) {
            if (!this._ship) {
                game.Globals.ship = this._ship = new game.Ship();
                this._ship.addToLayer(this);
            }
        } else if (game.Config.getLifeCount() <= 0 ) {
            this.unscheduleUpdate();
            var gameOver = cc.CallFunc.create(this.gameOver);
            this.runAction(cc.Sequence.create(cc.DelayTime.create(0.2), gameOver));
        }
    },

    collide:function(a, b) {
        if(!a || !b) {
            return false;
        }
        var aRect = a.collideRect();
        var bRect = b.collideRect();
        //cc.intersect()
        if (cc.rectIntersectsRect(aRect, bRect)) {
            return true;
        }
        return false;
    },


    gameOver:function() {
        var scene = new game.GameOverScene();
        scene.setData({
            score : game.Config.getScoreValue()
        });
        cc.director.runScene(cc.TransitionFade.create(1.2, scene));
    },

    getShip:function(){
        return this._ship;
    },


});